package com.snow.order.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.snow.order.biz.FoodBiz;
import com.snow.order.entity.Food;
import com.snow.order.log.Log;

public class ShopShowSlt extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ShopShowSlt() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//  第一步：获取用户的输入信息 
		String pageNo = request.getParameter("pageNo");
		int iPageNo = 1;
		if (pageNo != null) {
			iPageNo = Integer.parseInt(pageNo);
		}
		//  第二步：调用JavaBean
		FoodBiz biz = null;
		List<Food> fdList = null;
		try {
			biz = new FoodBiz();
			fdList = biz.getAllFood(iPageNo); // 提取了所有菜品
			int pageCount=biz.getPageCount(); //提取菜品页数
			//  第三步：传值 
			request.setAttribute("FoodList", fdList);
			request.setAttribute("pageNo", new Integer(iPageNo));
			request.setAttribute("pageCounter", new Integer(pageCount));
			//  第四步：选择一个界面对用户进行响应
			request.getRequestDispatcher("/shop/ShopShow.jsp").forward(request,
					response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.logger.error(e.getMessage());
			request.setAttribute("errMsg", "系统繁忙，请稍后再连接。。。");
			request.getRequestDispatcher("/error.jsp").forward(
					request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
